<?php 
include("common.php");
$search='';
$key=isset($_GET['key'])?$_GET['key']:'';
if($key!=''){
	$search.= " where book_name like '%".$key."%'";
}
    $Page_size=10; 
    $sql = "select * from book ".$search." order by id asc";
	$result = mysql_query($sql);
	$count = mysql_num_rows($result); 
$page_count = ceil($count/$Page_size); 
$init=1; 
$page_len=7; 
$max_p=$page_count; 
$pages=$page_count;
	$rows = @mysql_num_rows($result);	

	if(isset($_GET['action']) && $_GET['action']=='delete'){
	    $sql = "delete from book where id =".$_GET['id']."";
		$result = mysql_query($sql);
		$rows=@mysql_affected_rows();
		if($rows<1){
			echo "<script>alert('删除失败！');window.location.href='student.php';</script>";
		}else{
			echo "<script>alert('删除成功！');window.location.href='student.php';</script>";
		}
	}

?>

 <div class="fr righter">
		  <div class="title">
		    <h1 align="center">查看图书信息</h1>
	      </div>
		  
		  <div class="search" style=" margin-left:30px;">
			  <form action="search.php" method="get" class="fl">
					<input type="text" name="key" value="<?php echo $key ?>"/>
					<input type="submit" value="搜索">
			  </form>
			  
		  </div>

		  <?php
 $cha=mysql_query("select * from book")or die('查询失败！');
 $nums=mysql_num_rows($cha);   //获取记录总数
 //分页显示
 $start=0;           //开始显示的记录数
 $nums_per=5;        //定义每页显示的记录数
 if(empty($_GET['start']))
 {
  $start=0;
 }
 else
  {
   $start=$_GET['start'];
  }
 $cha2=mysql_query("select * from book limit $start,$nums_per");
 $nums2=mysql_num_rows($cha2);  //每次最多输出记录数
 echo'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;共有图书:'.$nums.'本书'.'<br>';
 echo"<table border=1 width=96%><tr><td>图书编号</td><td>图书名称</td><td>图书价格</td><td>作者</td><td>出版社</td><td>出版时间</td></tr>";
 for ($i=0; $i<$nums2; $i++)                              //逐条输出插入的记录
 {
  $result=mysql_fetch_array($cha2);
  echo"<tr><td>$result[1]</td><td>$result[2]</td>";
  echo "<td>$result[3]</td><td>$result[4]</td>";
  echo "<td>$result[5]</td><td>$result[6]</td>";
				echo "</tr>";
  
 }
 echo"</table>";
 if($nums_per>=$nums)//如果每页显示的记录数大于总记录数则只有一页
 {
  echo"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当前只有一页";
 }
/*如果每页显示的记录数小于总记录数则只有一页，上一页开始显示的记录数等于当前记录数加于每页显示的记录数，下一页开始显示的记录数等于当前记录数减于每页显示的记录数*/
 else
 {                      
  $prev=$start-$nums_per;
  if($prev<0)
    {
     $prev=0;
    }
    $last=$start+$nums_per;
  if($last>$nums)
    {
  $last=$nums;
 }
//不是第一页就显示上一页，不是最后一页就显示下一页
 if($start==0)
 {
  echo"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href=".$_SERVER['PHP_SELF']."?page=1>首&nbsp&nbsp页</a>";
  echo"&nbsp&nbsp第一页&nbsp&nbsp";
 }
 else
 {
	 echo"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href=".$_SERVER['PHP_SELF']."?page=1>首&nbsp&nbsp页</a>";
  echo"<a href=".$_SERVER['PHP_SELF']."?start=$prev>&nbsp&nbsp上一页</a>&nbsp&nbsp&nbsp";
 }
 if($last==$nums)
 {
  echo"最后一页&nbsp&nbsp&nbsp";
 }
 else
 {
  echo"<a href=".$_SERVER['PHP_SELF']."?start=$last>下一页</a>";
 }
  }
?>

	  </div>
  </div>
  
 </body>
</html>
 



